home *** CD-ROM | disk | FTP | other *** search
/ Aminet 41 / Aminet 41 (2001)(Schatztruhe)[!][Feb 2001].iso / Aminet / docs / hyper / BuffyGuide.lha / BuffyGuide / source / Search.core.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-26  |  6.9 KB  |  256 lines

  1. /* -----------------------------------------------------------
  2.   $VER: Search.core.c 1.1 (21.05.2000)
  3.  
  4.   core rotines for the BuffyGuideSearch
  5.  
  6.   (C) Copyright 2000 Matthew J Fletcher - All Rights Reserved.
  7.   amimjf@connectfree.co.uk - www.amimjf.connectfree.co.uk
  8.  ------------------------------------------------------------ */
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <time.h>
  13. #include <signal.h>
  14. #include <ctype.h>
  15.  
  16. #include "Search.h" // thats ours
  17.  
  18.  
  19. // --------------------------
  20. // top level search procedure
  21. // --------------------------
  22. void do_search(char *argv[])
  23. {
  24.     // ------------------------------
  25.     // does all event handeling, for
  26.     // menu operations and quit events
  27.     // ------------------------------
  28.  
  29.     // delete file
  30.     remove("BuffyResults.guide");
  31.  
  32.     printf("#-----------------------------#\n");
  33.     printf("#     BuffyGuideSearch        #\n");
  34.     printf("#      version 1.0.2          #\n");
  35.     printf("#-----------------------------#\n");
  36.     printf("\n");
  37.  
  38.     // get search string (picky)
  39.     do {
  40.         printf("Enter search string:\n");
  41.         gets(searchstring);
  42.         if (searchstring == NULL) // dough !!
  43.             printf("Please enter a valid search string !\n");
  44.  
  45.     } while (searchstring == NULL);
  46.  
  47.     printf("Searching for (%s) in Buffy.guide...\n",searchstring);
  48.  
  49.     // start timeing search
  50.     start = clock(); // the beginning
  51.  
  52.     // create break handeler
  53.     signal(SIGINT, brkfunc);
  54.     // tell user
  55.     printf("Press ctrl-c to abort, search will be saved\n");
  56.                                                    
  57.     // ------------------------------------------------
  58.     // Open files (Buffy.guide and T:BuffyResults.guide
  59.     // ------------------------------------------------
  60.  
  61.     // they should be in our path
  62.     guide_fp = fopen(argv[1],"r");
  63.         if (guide_fp == NULL) { // not found
  64.             printf("Buffy.guide not found in progdir:\n");
  65.             exit(0);
  66.             }
  67.  
  68.     temp_fp = fopen("BuffyResults.guide","w");
  69.         if (temp_fp == NULL) { // not found
  70.             printf("Could not create search results file:\n");
  71.             exit(0);
  72.             }
  73.  
  74.     // writes search result file headers
  75.     write_header();
  76.  
  77.     // ----------------------------
  78.     // this is where it all happens
  79.     // ----------------------------
  80.     parse_guide();
  81.  
  82.     // thats all buffy !! -stop search
  83.  
  84. } // end do_search
  85.  
  86.  
  87. // --------------------
  88. // if processing broken
  89. // --------------------
  90. void brkfunc(int signo)
  91. {
  92.  
  93.     if (DEBUG ==1) // no need to tell user
  94.     printf("signal %d occured\n", signo);
  95.  
  96.     printf("Search Aborted !!\n");
  97.  
  98.     // generic closedown proc
  99.     end_search();
  100. } // end brkfunc
  101.  
  102.  
  103. // -----------------
  104. // parses guide file
  105. // -----------------
  106. void parse_guide(void)
  107. {
  108. char *token;
  109. char stuff[1000];
  110. char junkstr[1000];
  111.  
  112. // even long lines are no problem now !!
  113. char buffer[3000];
  114. int eof,catch,i=0,x=0,dup;
  115.  
  116. // from arexx
  117. char resultnode[1000];
  118. char resultnodename[1000];
  119. char resultstring[1000];
  120.  
  121.     // ----------------------------
  122.     // search file for string, make
  123.     // links to all found items
  124.     // ----------------------------
  125.  
  126.     // begin guide search
  127.     do {
  128.         if (fgets(buffer,sizeof(buffer),guide_fp) == NULL) {
  129.             eof = 1;
  130.             if(DEBUG==1)
  131.             printf("EOF at line %d\n",lines);
  132.  
  133.             // break here
  134.             end_search();
  135.             }
  136.  
  137.     // begin string search - finds first element
  138.     strtok(buffer," "); // delimiter is space
  139.  
  140.     catch=0,dup=0;
  141.     do { // check for search string
  142.  
  143.         // get next element of string buffer
  144.         token = strtok(NULL," ");
  145.  
  146.         if ((strstr(buffer,"@node")!=NULL) ||
  147.             (strstr(buffer,"@NODE")!=NULL)) {
  148.             if (catch==0) {
  149.             // one more
  150.             nodes++;
  151.             // node link is next
  152.             strcpy(resultnode,token); // node link
  153.  
  154.             // remove "" quotes
  155.             i=0,x=0;
  156.             do {
  157.                  if (resultnode[x] != '"') {
  158.                     junkstr[i] = resultnode[x];
  159.                     i++; x++;
  160.                     }
  161.                  else x++;
  162.  
  163.             } while (resultnode[x] != NULL);
  164.  
  165.             // copy null
  166.             junkstr[i] = 0;
  167.             strcpy(resultnode,junkstr);
  168.  
  169.  
  170.             token = strtok(NULL,"\"");
  171.             strcpy(resultnodename,token); // node title
  172.  
  173.             // fiddles,...
  174.             token = strtok(NULL," ");
  175.             catch=1; // no need to do something twice
  176.             }
  177.  
  178.             if(DEBUG==1)
  179.             printf("(%s) (%s) on line %d\n",resultnode,resultnodename,lines);
  180.             }
  181.  
  182.         
  183.         if ((CASE ==1) && (dup ==0)) // sensitive
  184.             if (strcmp(token,searchstring) == 0) {
  185.  
  186.                 dup=1; // dont search same line twice
  187.                 strcpy(resultstring,token); // matched string
  188.                 matchesfound++;
  189.  
  190.                 // write link to file
  191.                 sprintf(stuff," \"%s\"   in @{\"%s\" LINK \"Buffy.guide/%s\"} @{b}(Match %d / line %d)@{ub}\n",resultstring,resultnodename,resultnode,matchesfound,lines);
  192.                 fputs(stuff,temp_fp);
  193.  
  194.  
  195.                 if(DEBUG==1) {
  196.                     printf("Search String (%s) found on line %d\n",token,lines);
  197.                     printf("%s LINK Buffy.guide/%s\n",resultnodename,resultnode);
  198.                     }
  199.                 }
  200.  
  201.         if ((CASE ==0) && (dup ==0))// insensitive
  202.             #if defined ( _DCC ) || defined ( __BORLANDC__ ) || defined ( __VBCC__ )
  203.             if (stricmp(token,searchstring) ==0) {
  204.             #else
  205.             if (strcasecmp(token,searchstring) ==0) {
  206.             #endif
  207.  
  208.                 dup=1; // dont search same line twice
  209.                 strcpy(resultstring,token); // matched string
  210.                 matchesfound++;
  211.  
  212.                 // write link to file
  213.                 sprintf(stuff," \"%s\"   in @{\"%s \" LINK \"Buffy.guide/%s\"} @{b}(Match %d / line %d)@{ub}\n",resultstring,resultnodename,resultnode,matchesfound,lines);
  214.                 fputs(stuff,temp_fp);
  215.  
  216.                 if(DEBUG==1) {
  217.                     printf("Search String (%s) found on line %d\n",token,lines);
  218.                     printf("%s LINK Buffy.guide/%s\n",resultnodename,resultnode);
  219.                     }
  220.                 }
  221.  
  222.     } while (token != NULL);
  223.     // end string search
  224.  
  225.     lines++; // counting lines
  226.     } while (eof != 1);
  227.     // end guide search
  228.  
  229. } // end parse guide
  230.  
  231.  
  232. // -------------------------------
  233. // stops searching and generates
  234. // correct AmigaGuide at that time
  235. // -------------------------------
  236. int end_search(void)
  237. {
  238.     end = clock(); // the end
  239.     total = (end - start); // time diffrence
  240.  
  241.     // puts all stuff needed to complete AmigaGuide file
  242.     write_end();
  243.     // Close files
  244.     fclose(guide_fp);
  245.     fclose(temp_fp);
  246.  
  247.     // Multiview results
  248.     if (system("sys:Utilities/MultiView Buffy:BuffyResults.guide") == -1) // error
  249.         printf("Could Not Run Multiview on BuffyResults.guide\n");
  250.  
  251.     exit(0);
  252.  
  253. } // end end_search
  254.  
  255.  
  256.